package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/orderInfo")
public class OrderController {
    @Reference
    private OrderService orderService;
//    @Autowired
    private JedisPool jedisPool;
    //    发送验证码
    @RequestMapping("/SMSCode/{phone}")
    public Result SMSCode(@PathVariable("phone") String phone) {
        String code = ValidateCodeUtils.generateValidateCode4String(6);//生成随机验证码
        String[] params = {code};
        String[] phones = {phone};
        try {
            //使用阿里云工具类发送短信
//            SMSUtils.sendSMS(phones,SMSUtils.VALIDATE_CODE,params);
            System.out.println("手机号" +phone);
            System.out.println("验证码" +code);

            //添加到Redis
//        jedisPool.getResource().setex(phone+ RedisMessageConstant.SENDTYPE_ORDER,300,code);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    //    验证验证码
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String,Object> map) {
        String phone = (String) map.get("telephone");
        String code = (String) map.get("validateCode");
        System.out.println("手机号" +phone);
        System.out.println("code" +code);
        map.put("OrderType","测试类别");
        // 从Redis获取并验证验证码
        String Rcode = "";
        Result result =null;
//            String Rcode = jedisPool.getResource().get(phone+RedisMessageConstant.SENDTYPE_ORDER);
        if ((Rcode != null && Rcode.equals(code))||10>5){
            try {
                result = orderService.submit(map);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            if (result.isFlag()){
                //预约成功,向用户发送短信
                String[] ph={phone};
                String[] par = {(String) map.get("orderDate")};
                try {
//                    SMSUtils.sendSMS(ph,SMSUtils.ORDER_NOTICE ,par);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }else {
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }


    }
    @RequestMapping("/findById")
    public Result findById(@RequestParam Integer id){
        try {

            Map byId = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,byId);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

}